<template>
  <div class="page-container">
    <!-- 顶部标题栏 -->
    <header class="details-header">
      <el-page-header content="查看企业详情" @back="$router.back()" />
    </header>
    <main class="details-main">
      <!-- 企业信息 -->
      <div class="info-container">
        <!-- 标题 -->
        <div class="title">企业信息：</div>
        <div class="info">
          <!-- 企业信息列表 -->
          <el-descriptions class="margin-top" :column="2" size="medium" :label-style="{ color: '#333', fontWeight: 'bold' }">
            <el-descriptions-item label="企业名称">{{ enterpriseDetails.name }}</el-descriptions-item>
            <el-descriptions-item label="法人">{{ enterpriseDetails.legalPerson }}</el-descriptions-item>
            <el-descriptions-item label="注册地址">{{ enterpriseDetails.registeredAddress }}</el-descriptions-item>
            <el-descriptions-item label="所在行业">{{ enterpriseDetails.industryName }}</el-descriptions-item>
            <el-descriptions-item label="营业执照">
              <el-image
                style="width: 200px"
                :src="enterpriseDetails.businessLicenseUrl"
                fit="contain"
                :preview-src-list="[enterpriseDetails.businessLicenseUrl]"
              />
            </el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <!-- 联系人信息 -->
      <div class="info-container">
        <!-- 标题 -->
        <div class="title">联系人信息：</div>
        <div class="info">
          <!-- 联系人信息列表 -->
          <el-descriptions class="margin-top" :column="2" size="medium" :label-style="{ color: '#333', fontWeight: 'bold' }">
            <el-descriptions-item label="企业联系人">{{ enterpriseDetails.contact }}</el-descriptions-item>
            <el-descriptions-item label="联系电话">{{ enterpriseDetails.contactNumber }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
      <!-- 租赁信息 -->
      <div class="info-container">
        <!-- 标题 -->
        <div class="title">租赁记录：</div>
        <!-- 租赁信息表单 -->
        <div class="info">
          <el-table :data="enterpriseDetails.rent" border :header-cell-style="{ background: '#f7f8f9', color: '#333', fontWeight: 'bold' }">
            <el-table-column type="index" label="序号" width="80" />
            <el-table-column label="租赁楼宇" prop="name" />
            <el-table-column label="合同状态" prop="name">
              <template #default="{ row }">
                {{ statusObj[row.status] }}
              </template>
            </el-table-column>
            <el-table-column label="租赁起止时间" width="200">
              <template #default="{ row }">
                {{ `${row.startTime} 至 ${row.endTime}` }}
              </template>
            </el-table-column>
            <el-table-column label="租赁合同">
              <template #default="{ row }">
                <el-link type="primary" :href="previewUrl + row.contractUrl" :underline="false" target="_blank">{{ row.contractName }}</el-link>
              </template>
            </el-table-column>
            <el-table-column label="录入时间" prop="createTime" width="160" />
            <el-table-column label="操作" width="150">
              <template #default="{ row }">
                <el-button type="text" size="medium" @click="onDownloadContract(row)">合同下载</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { getEnterpriseDetailsAPI } from '@/api/park/enterprise'
import { downloadAPI } from '@/api/download'

import FileSaver from 'file-saver'

export default {
  name: 'EnterpriseDetails',
  data() {
    return {
      // 企业详情数据
      enterpriseDetails: {},
      // 合同的租赁状态
      statusObj: {
        0: '待生效',
        1: '生效中',
        2: '已到期',
        3: '已退租'
      },
      previewUrl: 'https://view.officeapps.live.com/op/view.aspx?src=' // 预览合同文件url
    }
  },
  computed: {
    enterpriseId() {
      return this.$route.params.id
    }
  },
  async created() {
    if (this.enterpriseId) {
      // 获取企业信息详情
      await this.getEnterpriseDetails()
    }
  },
  methods: {
    // 获取企业信息详情
    async getEnterpriseDetails() {
      this.enterpriseDetails = await getEnterpriseDetailsAPI(this.enterpriseId)
      console.log(this.enterpriseDetails)
    },
    // 下载合同
    async onDownloadContract(row) {
      const res = await downloadAPI(row.contractId)
      FileSaver.saveAs(res, row.contractName)
    }
  }
}
</script>

<style scoped lang="scss">
.page-container {
  .details-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    margin-bottom: 30px;
    height: 64px;
    background-color: #fff;
    border-bottom: 1px solid #eee;

    ::v-deep .el-page-header {
      .el-page-header__title {
        font-size: 16px;
        font-weight: 600;
        color: #444;
      }

      .el-page-header__content {
        font-size: 16px;
        font-weight: 600;
        color: #444;
      }
    }
  }

  .details-main {
    padding-left: 50px;

    .info-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        font-weight: 600;
        color: #444;
      }

      .info {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        ::v-deep .el-descriptions-item {
          padding-bottom: 20px;
        }
      }
    }
  }
}
</style>
